<template>
  <el-card>
    <template #header>
      <h3>修改密码</h3>
    </template>

    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="70px"
      class="demo-ruleForm"
      size="mini"
    >
      <el-form-item label="原密码" prop="oldPass">
        <el-input v-model="ruleForm.oldPass"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { validatePass, checkPass1, checkPass2 } from '@/utils/validate.js'

export default {
  data() {
    return {
      ruleForm: {
        oldPass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        oldPass: [{ validator: validatePass, trigger: 'blur' }],
        pass: [{ validator: checkPass1(this, 'checkPass'), trigger: 'blur' }],
        checkPass: [{ validator: checkPass2(this, 'pass'), trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(s) {
      this.$refs[s].validate(valid => {
        if (valid) {
          this.$message({
            message: '验证通过，正在发起请求...',
            type: 'success'
          })
        } else {
          this.$message.error('请按照规则填写表单！！')
          return false
        }
      })
    },
    resetForm(s) {
      this.$refs[s].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 249px;
}
</style>
